package logic

import (
	"context"
	"fmt"
	"strconv"
	"taobao/internal/svc"
	"taobao/models/cache"
	"taobao/taobao"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *taobao.ListCartRequest) (*taobao.ListCartResponse, error) {
	// todo: add your logic here and delete this line
	patten := fmt.Sprintf("cart:%d:*", in.UserId)
	keys := cache.Rdb.Keys(l.ctx, patten).Val()
	fmt.Println(keys)
	var carlist []*taobao.ListCart
	for _, i2 := range keys {
		goodsmap := cache.Rdb.HGetAll(l.ctx, i2).Val()
		fmt.Println(goodsmap)
		userId, _ := strconv.Atoi(goodsmap["user_id"])
		goodId, _ := strconv.Atoi(goodsmap["good_id"])
		goods_price, _ := strconv.ParseFloat(goodsmap["price"], 64)
		stock, _ := strconv.Atoi(goodsmap["stock"])
		status, _ := strconv.Atoi(goodsmap["status"])
		checkStaus, _ := strconv.Atoi(goodsmap["check_status"])

		carlist = append(carlist, &taobao.ListCart{
			UserId:     int64(userId),
			GoodsTitle: goodsmap["goods_title"],
			GoodsPrice: float32(goods_price),
			Stock:      int64(stock),
			GoodsCover: goodsmap["gcover"],
			Status:     strconv.FormatInt(int64(status), 10),
			CheckStaus: int64(checkStaus),
			GoodsId:    int64(goodId),
		})
	}
	return &taobao.ListCartResponse{
		List: carlist,
	}, nil
}
